package com.glodon.paas.activity.dao.mongo;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.glodon.paas.activity.dao.ActivityDao;
import com.glodon.paas.activity.dao.domain.ActivityEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.glodon.paas.util.ObjectUtil;
import com.google.code.morphia.query.Query;

public class ActivityDaoMongoImpl extends MongoDBDao<ActivityEntity, String> implements ActivityDao {

    private static Logger LOGGER       = LoggerFactory.getLogger(ActivityDaoMongoImpl.class);

    @Override
    public String insertSelective(ActivityEntity entity) {
        Map<String, Object> map = entity.getExtension();
        if (map != null && LOGGER.isDebugEnabled()) {
            LOGGER.debug("map=" + ObjectUtil.toJson(map));
        }
        return super.insert(entity);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return super.deleteById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ActivityEntity record) {
        // super.save(record);
        // return 1;
        throw PaasServerExceptionFactory.nosupport("不支持的方法");
    }

    @Override
    public ActivityEntity selectByPrimaryKey(String id) {
        return super.selectById(id);
    }

    @Override
    public List<ActivityEntity> select(
                                String receiverId, 
                                Integer appId, 
                                Integer moduleId, 
                                Long activityType,
                                String itemId,
                                String itemType,
                                String subItemId,
                                String actorId,
                                long nearerTime, // receiveTime毫秒
                                long olderTime,
                                int count
                                ) {
        // 条件
        Query<ActivityEntity> q = this.createQuery();

        if (StringUtils.isNotBlank(receiverId)) {
            q.filter("receiverId", receiverId);
            // q.field("userId").equals(userId);
        }
        if (appId != null) {
            q.filter("appId", appId);
        }
        if (moduleId != null) {
            q.filter("moduleId", moduleId);
        }
        if (activityType != null) {
            q.filter("activityType", activityType);
        }
        if (StringUtils.isNotBlank(itemId)) {
            q.filter("itemId", itemId);
        }
        if (StringUtils.isNotBlank(itemType)) {
            q.filter("itemType", itemType);
        }
        if (StringUtils.isNotBlank(subItemId)) {
            q.filter("subItemId", subItemId);
        }
        if (StringUtils.isNotBlank(actorId)) {
            q.filter("actorId", actorId);
        }
        if (nearerTime > 0) {
            q.filter("rt <= ", new Date(nearerTime));
        }
        if (olderTime > 0) {
            q.filter("rt >= ", new Date(olderTime));
        }
        
        q.order("-rt");
        
        if (count > 0) {
            q.limit(count);
        }

        long begin = System.currentTimeMillis();
        List<ActivityEntity> list = q.asList();
        long end = System.currentTimeMillis();
        LOGGER.debug("list.size()=" + list.size());
        LOGGER.debug("time=" + (end - begin));
        return list;
    }

}
